<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    :style="isRoot ? 'height:40px' : 'width:100%'"
  >
    <el-col>
      <span>{{ isRoot ? "传智教育" : treeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addHandler">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editHandler">编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="delHandler"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentInfo } from '@/api/department'
export default {
  name: 'TreeTools',
  props: {
    //   外部传入的这行数据
    treeData: {
      type: Object,
      default: () => ({})
    },
    // 是否为顶级
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    async delHandler() {
      // 弹出框
      await this.$confirm('确认删除了吗？')
      // 发送请求删除
      await delDepartmentInfo(this.treeData.id)
      //   提示成功
      this.$message.success('删除成功')
      // 更新列表
      this.$emit('update')
    },
    // 添加子部门
    addHandler() {
      // 通知父组件打开弹窗
      this.$emit('addDept', this.treeData)
    },
    // 修改部门
    editHandler() {
      this.$emit('editDept', this.treeData)
    }
  }
}
</script>
<style></style>
